const con=require("./db.js")
module.exports={
    testAPI:(req,res)=>{
        res.send("测试成功")
    },
    getallhero:(req,res)=>{
        let sql="select * from heros order by id asc;";
        con.query(sql,(err,result)=>{
            if(err) return res.send({status:500,msg:err.message,data:null});
            res.send({status:200,msg:"查询成功",data:result})
        })
    },
    addhero:(req,res)=>{
        let hero=req.body;
        let dt=new Date();
        let y=(dt.getFullYear()).toString().padStart(2,"0");
        let m=(dt.getMonth()+1).toString().padStart(2,"0");
        let d=(dt.getDate()).toString().padStart(2,"0");
        let hh=(dt.getHours()).toString().padStart(2,"0");
        let mm=(dt.getMinutes()).toString().padStart(2,"0");
        let ss=(dt.getSeconds()).toString().padStart(2,"0");
        hero.ctime=`${y}-${m}-${d} ${hh}:${mm}:${ss}`;
        let sql1="insert into heros set ?";
        con.query(sql1,hero,(err,result)=>{
            if(err) return res.send({status:500,msg:err.message,data:null});
            res.send({status:200,msg:"增加成功",data:result});
        })
    },
    gethero:(req,res)=>{
        let id=req.params.id;
        let sql2="select * from heros where id =?";
        con.query(sql2,id,(err,result)=>{
            if(err) return res.send({status:500,msg:err.message,data:null});
            res.send({status:200,msg:"查询成功",data:result});
        })
    },
    updatehero:(req,res)=>{
        let id=req.params.id;
        let hero=req.body;
        let sql3="update heros set ? where id =?";
        con.query(sql3,[hero,id],(err,result)=>{
            if(err) return res.send({status:500,msg:err.message,data:null});
            res.send({status:200,msg:"更新英雄成功",data:result});
        })
    },
    deletehero:(req,res)=>{
        let id=req.params.id;
        let isdel=req.params.isdel==1?0:1;
        let sql4="update heros set isdel=? where id =?";
        con.query(sql4,[isdel,id],(err,result)=>{
            if(err) return res.send({status:500,msg:err.message,data:null});
            res.send({status:200,msg:"软删除英雄成功",data:result});
        })
    },
    deleteyx:(req,res)=>{
        let id=req.params.id;
        let sql5="delete from heros where id=?";
        con.query(sql5,id,(err,result)=>{
            if(err) return res.send({status:500,msg:err.message,data:null});
            res.send({status:200,msg:"硬删除英雄成功",data:result}); 
        })
    }
}